package com.hy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hy.common.Result;
import com.hy.entity.Teacher;
import com.hy.service.SysTeacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/admin/teacher")
public class SysTeacherController {
    @Autowired
    private SysTeacher teacherService;

    // 将 uploadPath 定义为类成员变量，@Value 才能正确注入
    @Value("${upload.teacher-avatar-path}")
    private String uploadPath;

    /**
     * 讲师上传头像
     * @param file
     * @return
     */
    @PostMapping("/uploadTeacherAvatar")
    public Result<String> uploadTeacherAvatar(@RequestParam("file") MultipartFile file) {
        try {
            String originalFilename = file.getOriginalFilename();
            if (originalFilename == null || originalFilename.isEmpty()) {
                return Result.error("文件名为空");
            }
            String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName = UUID.randomUUID().toString() + fileSuffix;

            File destDir = new File(uploadPath);
            if (!destDir.exists()) {
                destDir.mkdirs(); // 创建目录
            }
            File destFile = new File(uploadPath + newFileName);
            file.transferTo(destFile); // 保存文件

            String accessUrl = "/teacher-avatar/" + newFileName;
            return Result.success(accessUrl); // 返回可访问路径
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("上传失败：" + e.getMessage());
        }
    }

    // 创建老师信息
    @PostMapping("/createTeacher")
    public Result<Teacher> createTeacher(@RequestBody Teacher teacher) {
        try {
            return teacherService.createTeacher(teacher);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("创建老师信息失败");
    }

    // 获取所有老师信息
    // 分页获取老师信息
    @GetMapping("/getTeacherPage")
    public Result<IPage<Teacher>> getTeacherPage(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String teacherName,
            @RequestParam(required = false) Integer minLikes,
            @RequestParam(required = false) Integer maxLikes) {
        try {
            return teacherService.getTeacherPage(pageNum, pageSize, teacherName, minLikes, maxLikes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("分页获取教师信息失败");
    }

    // 根据 ID 获取老师信息
    @GetMapping("/getTeacherById")
    public Result<Teacher> getTeacherById(@RequestParam("id") Long id) {
        try {
            return teacherService.getTeacherById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("未找到该老师信息");
    }

    // 更新老师信息
    @PostMapping("/updateTeacher")
    public Result<Teacher> updateTeacher(@RequestBody Teacher teacher) {
        try {
            return teacherService.updateTeacher(teacher);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("更新老师信息失败");
    }

    // 根据 ID 删除老师信息
    @DeleteMapping("/deleteTeacher")
    public Result<String> deleteTeacher(@RequestParam("id") Long id) {
        try {
            return teacherService.deleteTeacher(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("删除老师信息失败");
    }
}
